package com.hddf.project.auth.dao_impl;

import java.util.List;
import java.util.Map;

import com.hddf.project.auth.dao.UserDao_MongoDB;
import com.hddf.project.auth.pojos.User;
import org.kommii.common_utils.Token_Generator;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class UserDaoImpl_MongoDB implements UserDao_MongoDB{
	
	public boolean addWebTokenAndAESKey(BasicDBObject obj, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(obj.get("user_id")).get();
		BasicDBObject doc = new BasicDBObject(); 
		doc.put("$set", obj);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}
	
	public void addAppKeyPair(int user_id,String pubk,Map prik_map,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user_id).get();
		DBObject entity = c.findOne(query);
		entity.put("pubk",pubk);
		entity.put("prik_map",prik_map);
		c.update(query, entity);
		//c.findAndModify(query, update);
		return ;
	}
	
	public String addWebToken(String username,long webtoken_deadline,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("username").is(username).get();
		DBObject entity = c.findOne(query);
		String web_token = Token_Generator.getUUID_Token();
		entity.put("web_token",web_token);
		entity.put("webtoken_lifetime",webtoken_deadline);
		c.update(query, entity);
		return web_token;
	}
	
	public String addWebToken(int user_id,long webtoken_deadline,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user_id).get();
		DBObject entity = c.findOne(query);
		String web_token = Token_Generator.getUUID_Token();
		entity.put("web_token",web_token);
		entity.put("webtoken_lifetime",webtoken_deadline);
		c.update(query, entity);
		return web_token;
	}
	
	public String addAppToken(String username,long apptoken_deadline,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("username").is(username).get();
		DBObject entity = c.findOne(query);
		String app_token = Token_Generator.getUUID_Token();
		entity.put("app_token",app_token);
		entity.put("apptoken_lifetime",apptoken_deadline);
		c.update(query, entity);
		return app_token;
	}
	
	public String addAppToken(int user_id,long apptoken_deadline,DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user_id).get();
		DBObject entity = c.findOne(query);
		String app_token = Token_Generator.getUUID_Token();
		entity.put("app_token",app_token);
		entity.put("apptoken_lifetime",apptoken_deadline);
		c.update(query, entity);
		return app_token;
	}
	
	public DBObject getUserByAppToken(String app_token, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("app_token").is(app_token).get();
		DBObject entity = c.findOne(query);
		return entity;
	}
	
	/**
	 * 根据令牌获取用户的简要信息 (用户名,aes_key,用户id,权限,令牌截止时间)
	 * @param web_token
	 * @param c
	 * @return
	 */
	public DBObject getSimpleUserByWebToken(String web_token, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("web_token").is(web_token).get();
		BasicDBObject key = new BasicDBObject("username",1)
		.append("aes_key",1)
		.append("user_id", 1)
		.append("webtoken_lifetime",1)
		.append("web_token",1)
		.append("isopen",1)
		.append("permission",1)
		.append("_id", 0);
		DBObject entity = c.findOne(query,key);
		return entity;
	}
	
	/**
	 * 根据令牌获取用户的简要信息 (昵称,私钥,用户id,令牌截止时间)
	 * @param app_token
	 * @param c
	 * @return
	 */
	public DBObject getSimpleUserByAppToken(String app_token, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("app_token").is(app_token).get();
		BasicDBObject key = new BasicDBObject("fullname",1)
		.append("prik_map",1)
		.append("user_id", 1)
		.append("apptoken_lifetime",1)
		.append("_id", 0);
		DBObject entity = c.findOne(query,key);
		return entity;
	}
	
	/**
	 * 根据令牌获取用户的公钥
	 * @param app_token
	 * @param c
	 * @return
	 */
	public DBObject getPubKeyByAppToken(String app_token, DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("app_token").is(app_token).get();
		BasicDBObject key = new BasicDBObject("fullname",1)
		.append("pubk",1)
		.append("_id", 0);
		DBObject entity = c.findOne(query,key);
		return entity;
	}

	public DBObject getUserByWebToken(String web_token, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("web_token").is(web_token).get();
		//BasicDBObject fileds = new BasicDBObject("_id",0);
		//DBObject entity = c.findOne(query,fileds);
		DBObject entity = c.findOne(query);
		return entity;
	}
	
	public DBObject getDBObjectUserById(int id, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query=queryBuilder.put("user_id").is(id).get();
		DBObject entity = c.findOne(query);
		return entity;
	}

	public boolean delete(int id, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(id).get();
		//DBObject entity = c.findOne(query);
		//c.remove(entity).toString();
		//使用 findAndRemove 方法会把被删除的对象返回 
		DBObject obj = c.findAndRemove(query);
		if(null!=obj)
			return true;
		else
			return false;
	}

	public User getUserByFullName(String fullname, DBCollection c) {
		User u = null;
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query=queryBuilder.put("fullname").is(fullname).get();
		DBObject entity = c.findOne(query);
		if(null!=entity){
			u = new User();
			u.toUserObject(entity);
		}
		return u;
	}

	public User getUserById(int id, DBCollection c) {
		User u = null;
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query=queryBuilder.put("user_id").is(id).get();
		BasicDBObject fields = new BasicDBObject("_id",0);
		DBObject entity=c.findOne(query,fields);
		if(null!=entity){
			u = new User();
			u.toUserObject(entity);
		}
		return u;
		
	}

	public User getUserByName(String name, DBCollection c) {
		User u = null;
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("username").is(name).get();
		BasicDBObject fields = new BasicDBObject("_id",0);
		DBObject entity = c.findOne(query,fields);
		if(null!=entity){
			u = new User();
			u.toUserObject(entity);
		}
		return u;
	}

	public User getUserByUser_No(String user_no, DBCollection c) {
		User u = null;
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query=queryBuilder.put("user_no").is(user_no).get();
		DBObject entity=c.findOne(query);
		if(null!=entity){
			u = new User();
			u.toUserObject(entity);
		}
		return u;
	}

	public boolean save(User user, DBCollection c) {
		//user.set_id(super.generateID());
		//coll.insert(user.toDbObject(),WriteConcern.SAFE);
		c.insert(user.toDbObject(), WriteConcern.NORMAL);
		return true;
	}

	public boolean setUserOff(int id, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(id).get();
		BasicDBObject doc = new BasicDBObject();
		BasicDBObject obj = new BasicDBObject();
		obj.put("isopen",0);
		doc.put("$set", obj);
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}

	public boolean setUserOn(int id, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(id).get();
		BasicDBObject doc = new BasicDBObject();
		DBObject obj = new BasicDBObject();
		obj.put("isopen",1);
		doc.put("$set", obj);
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}

	public boolean update(User user, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user.getId()).get();
		BasicDBObject doc = new BasicDBObject(); 
		BasicDBObject obj = new BasicDBObject();
		obj.put("username", user.getUsername());
		obj.put("password", user.getPassword());
		obj.put("permission", user.getPermission());
		obj.put("department", user.getDepartment());
		obj.put("description", user.getDescription());
		obj.put("mobile_phone", user.getMobile_phone());
		doc.put("$set", obj);
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}
	
	public boolean updatePassword(User user, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user.getId()).get();
		BasicDBObject doc = new BasicDBObject(); 
		BasicDBObject obj = new BasicDBObject();
		obj.put("password", user.getPassword());
		doc.put("$set", obj);
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}
	
	public boolean update(DBObject obj, DBCollection c) {
		int user_id = (Integer)obj.get("user_id");
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(user_id).get();
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, obj);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}
	
	public boolean updateWebToken(DBObject obj, DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("user_id").is(obj.get("user_id")).get();
		BasicDBObject doc = new BasicDBObject(); 
		BasicDBObject o = new BasicDBObject();
		o.put("web_token",obj.get("web_token"));
		o.put("webtoken_lifetime", obj.get("webtoken_lifetime"));
		doc.put("$set", o);
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, doc);
		int rs = wr.getN();
		if(1==rs)
			return true;
		else
			return false;
	}

	public List<DBObject> getUserList(String start, String limit, int isopen,DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		if(isopen==1){
			isopen = 0;
		}else if(isopen==0){
			isopen = 1;
		}
		// notEquals 表示不等于 is 表示等于
		DBObject query = queryBuilder.put("isopen").notEquals(isopen).
							put("permission").notEquals("all").get();
		int st = 0;
		int li = 0;
		if(start!=null && limit!=null){
			st = Integer.parseInt(start);
			li = Integer.parseInt(limit);
		}
		
		BasicDBObject key = new BasicDBObject("username",1)
		.append("permission",1)
		.append("user_id", 1)
		.append("register_date",1)
		.append("last_login_date",1)
		.append("department",1)
		.append("description",1)
		.append("isopen", 1)
		.append("mobile_phone", 1)
		.append("_id", 0);
		
		//List<DBObject> list = c.find().toArray();
		// 1 代表正序 -1代表倒序  skip 代表从哪里开始(start) limit 代表获取多少个数据(limit)
		List<DBObject> list = c.find(query,key).skip(st).sort(new BasicDBObject("register_date", -1)).limit(li).toArray();
		return list;
		
	}

	public int getUserTotalCount(int isopen, DBCollection c) {
		
		QueryBuilder queryBuilder = QueryBuilder.start();
		if(isopen==1){
			isopen = 0;
		}else if(isopen==0){
			isopen = 1;
		}
		// notEquals 表示不等于 is 表示等于
		DBObject query = queryBuilder.put("isopen").notEquals(isopen).
							put("permission").notEquals("all").get();
		BasicDBObject key = new BasicDBObject("isopen",1)
								.append("_id", 0);
		int totalCount = c.find(query,key).length();
		return totalCount;
	}

	

	
}
